package bar.dark.lee.alarm.effect.question;

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import bar.dark.lee.alarm.R;
import bar.dark.lee.alarm.common.BDUtil;
import bar.dark.lee.alarm.effect.EffectActivity;
import bar.dark.lee.alarm.effect.EffectListActivity;
import bar.dark.lee.alarm.query.AlarmData;

public class EffectQuestionActivity extends EffectActivity {

	private int effectNumber;
	private boolean flagTestReal;
	private int selectNumber;
	
	private ViewGroup viewCount;
	private TextView textCount;
	
	private ViewGroup viewQuestion;
	private TextView textQuestion;
	private EditText editAnswer;
	private TextView textCorrect;
	private Button btnSend;
	
	private int result;
	private int correctCount = 3;
	
	private MediaPlayer mp;
	private AudioManager am;
	
	private int maxVolum;
	private int streamType = AudioManager.STREAM_RING;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.effectquestion_activity);
		
		Intent intent = getIntent();
		effectNumber = intent.getIntExtra(EffectListActivity.EFFECT_PLAY, -1);
		flagTestReal = intent.getBooleanExtra(EffectActivity.FLAG_TEST_REAL, false);
		selectNumber = intent.getIntExtra(EffectListActivity.EFFECT_SELECT, -1);
		
		if (flagTestReal)
		{
			TextView leftButton = (TextView) findViewById(R.id.superLeftButton);
			leftButton.setVisibility(View.INVISIBLE);
		}
		
		if (effectNumber == -1)
		{
			
		}
		else
		{
			super.setTitle(effectNumber);
		}
		
		setFindView();
		setDataSetting();
		setClickListener();
		
		if (selectNumber != -1)
		{
			startSound();
		}
		
		countdown();
	}
	
	private void startSound()
	{
		AlarmData data = query.getAlarmData(superContext, selectNumber + "");
		
		mp = new MediaPlayer();
		am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
		maxVolum = am.getStreamMaxVolume(streamType);
		am.setStreamVolume(streamType, maxVolum, AudioManager.STREAM_RING);
		
		mp.setAudioStreamType(streamType);
		
		try
		{
			mp.setDataSource(data.getSound());
			mp.prepare();
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		
		mp.start();
	}
	
	private void endSound()
	{
		if (mp != null)
		{
			if (mp.isPlaying())
			{
				mp.stop();
				mp.reset();
			}
		}
	}

	@Override
	public void setFindView() {

		viewCount = (ViewGroup) findViewById(R.id.effectQuestion_viewCount);
		textCount = (TextView) findViewById(R.id.effectQuestion_textCount);
		
		viewQuestion = (ViewGroup) findViewById(R.id.effectQuestion_viewQuestion);
		textQuestion = (TextView) findViewById(R.id.effectQuestion_textQuestion);
		editAnswer = (EditText) findViewById(R.id.effectQuestion_editAnswer);
		textCorrect = (TextView) findViewById(R.id.effectQuestion_textCorrect);
		btnSend = (Button) findViewById(R.id.effectQuestion_btnSend);
	}

	@Override
	public void setDataSetting() {
	}

	@Override
	public void setClickListener() {
		
		btnSend.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String answer = editAnswer.getText().toString();
				if (answer.length() == 0)
				{
					BDUtil.showDialogOK(superContext, "", gets(R.string.effectquestion_errordialog), null);
				}
				else
				{
					if (answer.equalsIgnoreCase(result+""))
					{
						playResult(true);
						
						Handler handler = new Handler()
						{
							@Override
							public void handleMessage(Message msg) {
								super.handleMessage(msg);
								
								if (correctCount == 1)
								{
									endSound();
									finish();
								}
								else
								{
									editAnswer.setText("");
									correctCount--;
									
									countdown();
								}
							}
						};
						
						handler.sendEmptyMessageDelayed(0, 2 * 1000);
					}
					else
					{
						playResult(false);
						
						Handler handler = new Handler()
						{
							@Override
							public void handleMessage(Message msg) {
								super.handleMessage(msg);
								
								editAnswer.setText("");
								
								countdown();
							}
						};
						
						handler.sendEmptyMessageDelayed(0, 2 * 1000);
					}
				}
			}
		});
	}

	@Override
	protected void tipButton() {
		
	}
	
	@Override
	public void onBackPressed() {
		
		if (flagTestReal)
		{
			
		}
		else
		{
			super.onBackPressed();
		}
	}
	
	private void countdown()
	{
		Handler handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				switch (msg.what) 
				{
					case 1:
						viewQuestion.setVisibility(View.INVISIBLE);
						viewCount.setVisibility(View.VISIBLE);
						textCount.setText("3");
						break;
					case 2:
						textCount.setText("2");
						break;
					case 3:
						textCount.setText("1");
						break;
					case 4:
						viewCount.setVisibility(View.INVISIBLE);
						viewQuestion.setVisibility(View.VISIBLE);
						
						textQuestion.setText(setQuestion());
						textCorrect.setText(correctCount+"");
						
						break;
					default:
						break;
				}
			}
		};
		
		for (int i=0; i<4; i++)
		{
			handler.sendEmptyMessageDelayed(i+1, i * 500);
		}
	}
	
	private String setQuestion()
	{
		int type = (int)(Math.random()*3);
		
		int first = (int)(Math.random()*100) + 1;
		int second = (int)(Math.random()*100) + 1;
		
		String returnValue = "";
		
		switch (type)
		{
			case 0:
				result = first + second;
				returnValue = first + " + " + second;
				break;
			case 1:
				if (first >= second)
				{
					result = first - second;
					returnValue = first + " - " + second;
				}
				else if (second > first)
				{
					result = second - first;
					returnValue = second + " - " + first;
				}
				break;
			case 2:
				if (second > 10)
				{
					second = second / 10;
				}
				result = first * second;
				returnValue = first + " X " + second;
				break;
			default:
				break;
		}
		
		return returnValue;
	}
	
	private void playResult(boolean result)
	{
		Intent intent = new Intent(superContext, QuestionResultDialog.class);
		intent.putExtra(QuestionResultDialog.QUESTION_RESULT, result);
		startActivity(intent);
	}
}
